<template>
  <div>
  <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
      </template>
    </FilterTable>

    <Drawer
     width="100%"
      :visible.sync="openAddParamPopup"
      :title="title"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <actionParam :row="row" :actionType="actionType" @close="closeHandler" />
    </Drawer>
  </div>
</template>

<script>
  import { findPartReturnParam,editPartReturnParam } from '@/api/partsmanage/partinfos/partParameter/index';
  import { dictionary } from '@/utils/dictMixin';
  import actionParam from './customPopup/actionParam';

  export default {
    name: "partParameter",
    mixins: [dictionary],
    components: {actionParam},
    data() {
      return {
        topFilterList: this.createFormList(),
        fetchParams: {xhrAbort: true},
        columns: this.createTableColumns(),
        fetchApi: findPartReturnParam,
        openAddParamPopup: false,
        title:"",
        actionType:"",
        row:{}

      };
    },
    methods: {
      closeHandler(){
      this.openAddParamPopup = false;
      //刷新页面
      this.fetchParams = Object.assign({}, this.fetchParams);

      },
      edit(row){
      this.openAddParamPopup = true;
      this.title = "编辑备件退货参数";
      this.actionType = "edit";
      this.row = row;
      },
      add(){
      this.openAddParamPopup = true;
      this.title = "新增备件退货参数";
      this.actionType = "add";
      this.row = null;
      },
      collapseHandle() {
        this.$nextTick(() => {
          this.BaseTable.EXECUTE_RESET_HEIGHT();
        });
      },
      changeHandle(val) {
        this.fetchParams = Object.assign({}, val);
      },
      createTableColumns () {
        return[
          {
              title: '序号',
              dataIndex: 'index',
              width: 70,
              sorter: true,
              align:"center",
              render: props => {
                return <span>{props.row.index + 1}</span>;
              }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 100,
            align:"center",
            render: (props, h) => {
              return (
                <div>
                  <el-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                    编辑
                  </el-button>
                </div>
              );
            }
          },
          {
            title: '退货类型',
            dataIndex: 'returnType',
            sorter: true,
            filter: true,
            dictItems: this.createDictList('3017'),
            align:"center"
          },
          {
            title: '退货折扣率(%)',
            dataIndex: 'discountRate',
            sorter: true,
            align:"center",
            editable: true,
            defaultEditable: true,
            editRequired: true,
            editType: 'number',
            editValidator:(rule, value, callback) => {
              var discountRate = parseInt(value.discountRate);
              if (discountRate === undefined || discountRate === '' || discountRate === null) {
                callback(new Error('值不能为空'));
              }
              var pattern = /^[+]{0,1}(\d+)$/;
              if (pattern.test(value.discountRate)) {

              }else{
                callback(new Error('请输入正整数'));
                return;
              }



              if (discountRate === undefined || discountRate === '' || discountRate === null) {
                callback(new Error('值不能为空'));
              }
              var g = /^\-?[0-9]\d*.?\d*$/;
              if (g.test(discountRate)) {
                if (100 >= discountRate > 0) {
                  callback();
                  editPartReturnParam(value).then(res =>{
                    this.$notify.success({title: '提示信息',message:'修改成功!'});
                    this.fetchParams = Object.assign({}, this.fetchParams);
                  }).catch(err =>{
                    this.$notify.error({title: '提示信息',message:err.errMsg||'修改失败!'});
                  })
                  return;
                }
              }
              callback(new Error('只能输入数字，且在0-100之间'));
            },
          },
          {
            title: '修改人',
            dataIndex: 'updatedName',
            sorter: true,
            align:"center"
          },
          {
            title: '修改日期',
            dataIndex: 'updatedAt',
            sorter: true,
            filter: true,
            filterType: 'date-range',
            dateFormat:'yyyy-MM-dd',
            align:"center"
          },
        ]
      },
      createFormList() {
        return [
          { 
            type: 'SELECT', 
            label: '退货类型', 
            fieldName: 'returnType', 
            placeholder: '请选择',
            itemList: this.createDictList('3017')
          },
        ]
      },
    },

  }
</script>

<style scoped>

</style>
